package com.youlai.boot.factory.service.impl;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.youlai.boot.factory.mapper.CuttingBedDetailsMapper;
import com.youlai.boot.factory.service.CuttingBedDetailsService;
import com.youlai.boot.factory.model.entity.CuttingBedDetails;
import com.youlai.boot.factory.model.form.CuttingBedDetailsForm;
import com.youlai.boot.factory.model.query.CuttingBedDetailsQuery;
import com.youlai.boot.factory.model.vo.CuttingBedDetailsVO;
import com.youlai.boot.factory.converter.CuttingBedDetailsConverter;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;

/**
 * 裁床明细服务实现类
 *
 * @author youlaitech
 * @since 2025-08-24 15:02
 */
@Service
@RequiredArgsConstructor
public class CuttingBedDetailsServiceImpl extends ServiceImpl<CuttingBedDetailsMapper, CuttingBedDetails> implements CuttingBedDetailsService {

    private final CuttingBedDetailsConverter cuttingBedDetailsConverter;

    /**
    * 获取裁床明细分页列表
    *
    * @param queryParams 查询参数
    * @return {@link IPage<CuttingBedDetailsVO>} 裁床明细分页列表
    */
    @Override
    public IPage<CuttingBedDetailsVO> getCuttingBedDetailsPage(CuttingBedDetailsQuery queryParams) {
        Page<CuttingBedDetailsVO> pageVO = this.baseMapper.getCuttingBedDetailsPage(
                new Page<>(queryParams.getPageNum(), queryParams.getPageSize()),
                queryParams
        );
        return pageVO;
    }

    /**
     * 获取裁床明细表单数据
     *
     * @param id 裁床明细ID
     * @return 裁床明细表单数据
     */
    @Override
    public CuttingBedDetailsForm getCuttingBedDetailsFormData(Long id) {
        CuttingBedDetails entity = this.getById(id);
        return cuttingBedDetailsConverter.toForm(entity);
    }

    /**
     * 新增裁床明细
     *
     * @param formData 裁床明细表单对象
     * @return 是否新增成功
     */
    @Override
    public boolean saveCuttingBedDetails(CuttingBedDetailsForm formData) {
        CuttingBedDetails entity = cuttingBedDetailsConverter.toEntity(formData);
        return this.save(entity);
    }

    /**
     * 批量新增裁床明细
     *
     * @param formDataList 裁床明细表单对象列表
     * @return 是否新增成功
     */
    @Override
    public boolean saveBatchCuttingBedDetails(List<CuttingBedDetailsForm> formDataList) {
        List<CuttingBedDetails> entityList = formDataList.stream()
                .map(cuttingBedDetailsConverter::toEntity)
                .collect(Collectors.toList());
        int insertCount = this.baseMapper.insertBatch(entityList);
        return insertCount > 0;
    }

    /**
     * 更新裁床明细
     *
     * @param id   裁床明细ID
     * @param formData 裁床明细表单对象
     * @return 是否修改成功
     */
    @Override
    public boolean updateCuttingBedDetails(Long id,CuttingBedDetailsForm formData) {
        CuttingBedDetails entity = cuttingBedDetailsConverter.toEntity(formData);
        return this.updateById(entity);
    }

    /**
     * 删除裁床明细
     *
     * @param ids 裁床明细ID，多个以英文逗号(,)分割
     * @return 是否删除成功
     */
    @Override
    public boolean deleteCuttingBedDetailss(String ids) {
        Assert.isTrue(StrUtil.isNotBlank(ids), "删除的裁床明细数据为空");
        // 逻辑删除
        List<Long> idList = Arrays.stream(ids.split(","))
                .map(Long::parseLong)
                .toList();
        return this.removeByIds(idList);
    }
}
